//#include <fstream>
#include <cstdlib>
#include "Task.h"
#include "streams.h"

//extern std::ofstream debug;

bool taskOccuped(const TargetTask& tasks, const Task& task, WeightTask& previousTask)
{
    TargetTask::const_iterator prev = tasks.find(task);
    if (prev == tasks.end())
        return false;

    previousTask = std::make_pair(prev->second.first, prev->second.second);
    return true;
}

int circular(int x0, int x1, int mod)
{
    return std::min( std::abs(x0-x1+mod) % mod, std::abs(x1-x0+mod) % mod);
}

int distanceFast(const Location& from, const Location& to, const Location& resolution)
{
    return  circular(from.row, to.row,resolution.row) +
            circular(from.col, to.col, resolution.col);
}



/*
template <typename T>
// return true if target found
bool FindNearestTarget(const Location& source, const LocationVector& destinations, const Location& resolution, WeightTask& result, T& t)
{

    OUT_PRINT_FUN "Source:" << source << " destinations_size " << destinations.size() << " resolution: " << resolution << std::endl;
    bool found = false;
    int dist_tmp , dist = resolution.row + resolution.col;

    FOR_EACH(dst,destinations)
    {
        dist_tmp = distanceFast(source, *dst, resolution);

        OUT_PRINT_FUN source << "<->" << *dst << " distance: " << dist_tmp << std::endl;

        if ( dist > dist_tmp)
        {
            found = true;
            dist = dist_tmp;
            result.first = *dst;
            result.second = dist;
        }
    }

    OUT_PRINT_FUN "result: " << found << std::endl;

    return found;
}

*/
